<?php
/*
 * 血压信息编辑及展示接口
 * */
class Apiv1_BloodController extends Ctrl_Apiatk{
    //编辑    
    public function editAction(){
        $p = $_REQUEST;
        $pId = empty($p['sid'])?Tool_Fnc::ajaxMsg('老人ID不能为空'):Tool_Fnc::safe_string($p['sid']);
        $pHighpressure = empty($p['high_pressure'])?'':Tool_Fnc::safe_string($p['high_pressure']);
        $pLowpressure = empty($p['low_pressure'])?'':Tool_Fnc::safe_string($p['low_pressure']);
        //$pStatus = empty($p['status'])?Tool_Fnc::ajaxMsg('血压状态不能为空'):Tool_Fnc::safe_string($p['status']);
        $pAt = empty($p['at'])?Tool_Fnc::ajaxMsg('时间节点不能为空'):Tool_Fnc::safe_string($p['at']);

        $tSMO = new SeniorModel;
        $tId = $tSMO->field('id')->where('id = '.$pId)->fRow()['id'];
        if(empty($tId)){Tool_Fnc::ajaxMsg('该老人不存在');}

        $tBPMO = new Blood_PressureModel;
        $tTime = time();
            $tData = array(
                'high_pressure' => $pHighpressure,
                'low_pressure' => $pLowpressure,
                'high_status' => $this->getHigh($pHighpressure),
                'low_status' => $this->getLow($pLowpressure),
                'created' => $tTime,
                'sid' => $pId,
                'at' => $pAt,
                'date' => date('Y-m-d',$tTime),
                'week' => $this->getTimeWeek($tTime),
            );
            if(!$tBPMO->insert($tData)){
                Tool_Fnc::ajaxMsg('编辑失败 001');
            }
        Tool_Fnc::ajaxMsg('编辑成功',1);
    }
    //展示
    public function listAction(){
        $p = $_REQUEST;
        $pId = empty($p['sid'])?Tool_Fnc::ajaxMsg('老人ID不能为空'):Tool_Fnc::safe_string($p['sid']);
        $pDate = empty($p['date'])?'':Tool_Fnc::safe_string($p['date']);
        $pAt = empty($p['at'])?Tool_Fnc::ajaxMsg('时间节点不能为空'):Tool_Fnc::safe_string($p['at']);

        $tSMO = new SeniorModel;
        $tId = $tSMO->field('id')->where('id = '.$pId)->fRow()['id'];
        if(empty($tId)){Tool_Fnc::ajaxMsg('该老人不存在');}

        $tBPMO = new Blood_PressureModel;
        //默认查询本周数据
        $time = time();
        $w_day=date("w",$time);
        if($w_day=='1'){
                $cflag = '+0';
                $lflag = '-1';
        }else{
                $cflag = '-1';
                $lflag = '-2';
        }
        //$weekstar = strtotime(date('Y-m-d',strtotime("$cflag week Monday", $time))); //本周一零点的时间戳
        //$weekend = strtotime(date('Y-m-d',strtotime("$cflag week Monday", $time)))+7*24*3600;//本周末零点的时间戳
        //$tWhere = ' sid = '.$pId.' and created > '.$weekstar.' and created < '.$weekend;
        $tWhere = ' sid = '.$pId.' and at = '.$pAt;
        if(!empty($pDate)){
           $tWhere = ' sid = '.$pId.' and date = "'.$pDate.'"'; 
        }
        $tDatas = $tBPMO->field('id,high_pressure,low_pressure,week,sid,high_status,low_status,created')->where($tWhere)->order('created desc')->limit('7')->fList();
            Tool_Fnc::ajaxMsg('',1,$tDatas);
    }
    //获取周几
    private function getTimeWeek($time, $i = 0) {
        $weekarray = array('1', '2', '3', "4", "5", "6", "7");
        $oneD = 24 * 60 * 60;
        return $weekarray[date("w", $time + $oneD * ($i-1))];
    }
    //获取高压状态
    private function getHigh($tHigh){
        if($tHigh >=120 && $tHigh <= 155){
            return $tStatus = 0; 
        }else if($tHigh >= 156 && $tHigh <= 159){
            return $tStatus = 1;
        }else if($tHigh >= 160 && $tHigh <= 180){
            return $tStatus = 2;
        }else if($tHigh >= 105 && $tHigh <= 119){
            return $tStatus = 3;
        }else if($tHigh >= 90 && $tHigh <= 104){
            return $tStatus = 4;
        }else{
            return $tStatus = 5;
        }
    }
    //获取低压状态
    private function getLow($tLow){
        if($tLow > 85 && $tLow <= 90){
            return $tStatus = 0;
        }else if($tLow > 90 && $tLow <= 99){
            return $tStatus = 1;
        }else if($tLow >= 100 && $tLow <= 110){
            return $tStatus = 2;
        }else if($tLow > 75 && $tLow <= 85){
            return $tStatus = 3;
        }else if($tLow > 60 && $tLow <= 74){
            return $tStatus = 4;
        }else{
            return $tStatus = 5;
        }
    }
}
?>
